package com.gc.fm;

import com.gc.db.Columns;
import com.gc.db.Tables;
import freemarker.template.Configuration;
import freemarker.template.Template;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class FmReadWrite {

   static Configuration config;
   static String basedir = "D://generateCode/";
    static{
        config = new Configuration();
        config.setClassForTemplateLoading(FmReadWrite.class,"/com/template");
        config.setDefaultEncoding("UTF-8");
    }

    public static boolean createDir(String dirpath,int model){
        File file = new File(dirpath);
        file.mkdirs();
        new File(dirpath+"/entity").mkdirs();
        new File(dirpath+"/service").mkdirs();
        new File(dirpath+"/service/impl").mkdirs();
        if(model == 1){
            new File(dirpath+"/dao").mkdirs();
            new File(dirpath+"/dao/impl").mkdirs();
            new File(dirpath+"/servlet").mkdirs();
            new File(dirpath+"/util").mkdirs();
        }else{
            new File(dirpath+"/mapper").mkdirs();
            new File(dirpath+"/controller").mkdirs();
        }
        System.out.println("文件夹创建成功！");
        return true;
    }
    public static void generate(Map<String,Object> map,String path,String filepath){
        try {
            Template template = config.getTemplate(path);
            //Writer out = new OutputStreamWriter(System.out);
            Writer out = new OutputStreamWriter(new FileOutputStream(filepath));
            template.process(map, out);
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        Tables tables = new Tables();
        Columns c1 = new Columns("stu_id","4","");
        Columns c2 = new Columns("stuname","1","");
        Columns c3 = new Columns("stuAge","5","");
        List<Columns> list = new ArrayList<>();
        list.add(c1);
        list.add(c2);
        list.add(c3);
        tables.setColums(list);
        tables.setName("student");
        tables.setPkColumns(c1);
        Map<String,Object> map = new HashMap<>();
        map.put("table",tables);
        String packagename = "com/gc/";
        createDir("com.gc",1);
        map.put("dir","com.gc");
        map.put("hasspring",true);
        map.put("hasmybatis",true);
        map.put("hasspringmvc",true);
       /* generate(map,"configurationfile/webxml.ftl",basedir+"web.xml");
        generate(map,"configurationfile/springxml.ftl",basedir+"spring.xml");
        generate(map,"configurationfile/springmvcxml.ftl",basedir+"springmvc.xml");
        generate(map,"configurationfile/mybatisconfigxml.ftl",basedir+"mybatisconfig.xml");*/
        generate(map,"configurationfile/mybatismapperxml.ftl",basedir+tables.getName()+"Mapper.xml");
        //createDir("com.gc");

    }
}
